package com.zhiyou100.controller;

import com.alibaba.fastjson.JSON;
import com.zhiyou100.service.UserService;
import com.zhiyou100.unit.SendMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import javax.servlet.http.HttpServletRequest;

@Controller
@RequestMapping("/pwd")
public class Password {
    @Autowired
    JedisPool jedisPool;
    @Autowired
    UserService userService;

    /**
     * @param phoneNum
     * @return
     * 接收到手机号码,发送短信,返回验证码
     * 将手机号码转成jsonString
     * 以验证码为key,手机号为value 存入缓存 设置失效时间为5分钟
     */
    @RequestMapping("/ajaxSendMsg")
    @ResponseBody
    public String forgetPwd(String phoneNum){
        System.out.println("接收到ajax请求"+phoneNum);
        String code = SendMessage.SendMessage(phoneNum);
        String jsonStringPhoneNum = JSON.toJSONString(phoneNum);
        Jedis jedis = jedisPool.getResource();
        jedis.set(code,jsonStringPhoneNum);
        jedis.expire(code,300);
        System.out.println("验证码是 :"+code);
        return "success";
    }

    /**
     * @param phoneNum
     * @param jspcode
     * @param request
     * @return
     * 通过输入的验证码将缓存中的手机号码取出
     * 注意:要将从缓存中的手机号(是一个json字符串)解析成String :JSON.parse()
     * 否则比对失败
     */
    @RequestMapping("/forget")
    public String forgetPwd(String phoneNum , String jspcode,  HttpServletRequest request){
        System.out.println("手机号是 : "+phoneNum);
        System.out.println("输入的验证码 : "+jspcode);
        Jedis jedis = jedisPool.getResource();
        String RedisPhoneNum = jedis.get(jspcode);
//        String StringPhoneNum = JSON.toJSONString(RedisPhoneNum);
//        JSONObject jsonObject = JSONObject.parseObject(RedisPhoneNum);
        Object parse = JSON.parse(RedisPhoneNum);
        System.out.println("获得缓存中的手机号json字符串是 :"+RedisPhoneNum);
        System.out.println("获得缓存中的手机号是(转成对象后) :"+parse);
        if (phoneNum.equals(parse)){
            System.out.println("验证码正确!");
            String password =  userService.findPasswordByTel(phoneNum);
            System.out.println("密码 : "+password);
            request.setAttribute("password",password);
            return "forward:/forget_pwd.jsp";
        }else {
            System.out.println("比对失败");
            request.setAttribute("msg","验证码错误");
            return "forward:/forget_pwd.jsp";
        }


    }

    @RequestMapping("/update")
    public String updatePassword(String jspcode,String phoneNum,String newPassword,HttpServletRequest request){
        System.out.println("更新密码 - 验证码 :"+jspcode+" -手机号码 :"+phoneNum+" - 新密码:"+newPassword);
        Jedis jedis = jedisPool.getResource();
        String RedisPhoneNum = jedis.get(jspcode);
        Object parse = JSON.parse(RedisPhoneNum);
        if (phoneNum.equals(parse)){
            int i = userService.updatePasswordByTel(newPassword,phoneNum);
            System.out.println("更新密码后   : "+i);
            request.setAttribute("updatePWDSuccess","密码更改成功!");
            return "forward:/forget_pwd.jsp";
        }else {
            request.setAttribute("updatePWDFailed","密码更改失败!");
            return "forward:/forget_pwd.jsp";

        }


    }



}
